package com.cskaoyan.shopping.controller;

import com.alibaba.fastjson.JSONObject;
import com.cskaoyan.mall.commons.result.ResponseData;
import com.cskaoyan.mall.commons.result.ResponseUtil;
import com.cskaoyan.shopping.constants.GlobalConstants;
import com.cskaoyan.shopping.dal.entitys.PanelContent;
import com.cskaoyan.shopping.dto.PanelContentDto;
import com.cskaoyan.shopping.dto.PanelDto;
import com.cskaoyan.shopping.dto.ProductCateDto;
import com.cskaoyan.shopping.service.IHomeService;
import com.cskaoyan.shopping.service.impl.cache.CacheManager;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

/**
 * @author ljh
 * @version 1.0
 * @describition:
 * @since 2022/9/20 19:34
 **/
@RestController
@RequestMapping("shopping")
public class ShoppingController {
    @Autowired
    IHomeService iHomeService;
    @Autowired
    CacheManager cacheManager;

    //    /shopping/homepage
    @GetMapping("homepage")
    public ResponseData homepage() {

        //先查询缓存
        String s = cacheManager.checkCache(GlobalConstants.HOMEPAGE_CACHE_KEY);
        List<PanelDto> panels = null;
        ResponseUtil<List<PanelDto>> listResponseUtil = new ResponseUtil<>();
        if (s != null) {
            panels = JSONObject.parseArray(s, PanelDto.class);
            return listResponseUtil.setData(panels);
        }
        panels = iHomeService.homepage();
        s = JSONObject.toJSONString(panels);
        cacheManager.setCache(GlobalConstants.HOMEPAGE_CACHE_KEY,s,GlobalConstants.HOMEPAGE_EXPIRE_TIME);


        return listResponseUtil.setData(panels);
    }

    @GetMapping("navigation")
    public ResponseData navigation(){
        //先查询缓存
        String s = cacheManager.checkCache(GlobalConstants.HEADER_PANEL_CACHE_KEY);
        List<PanelContentDto> cates = null;
        ResponseUtil<List<PanelContentDto>> listResponseUtil = new ResponseUtil<>();
        if(s != null){
            cates = JSONObject.parseArray(s, PanelContentDto.class);
            return listResponseUtil.setData(cates);
        }
        cates = iHomeService.navigation();
        s = JSONObject.toJSONString(cates);
        cacheManager.setCache(GlobalConstants.HEADER_PANEL_CACHE_KEY,s,GlobalConstants.HEADER_EXPIRE_TIME);

        return listResponseUtil.setData(cates);
    }

    @GetMapping("categories")
    public ResponseData categories(){
        //先查询缓存
        String s = cacheManager.checkCache(GlobalConstants.PRODUCT_CATE_CACHE_KEY);
        List<ProductCateDto> cates = null;
        ResponseUtil<List<ProductCateDto>> listResponseUtil = new ResponseUtil<>();
        if(s != null){
            cates = JSONObject.parseArray(s, ProductCateDto.class);
            return listResponseUtil.setData(cates);
        }
        cates = iHomeService.categories();
        s = JSONObject.toJSONString(cates);
        cacheManager.setCache(GlobalConstants.PRODUCT_CATE_CACHE_KEY,s,GlobalConstants.PRODUCT_CATE_EXPIRE_TIME);
        return listResponseUtil.setData(cates);
    }


    @GetMapping("recommend")
    public ResponseData recommend(){
        //先查询缓存
        String s = cacheManager.checkCache(GlobalConstants.RECOMMEND_PANEL_CACHE_KEY);
        PanelDto cate = null;
        List<PanelDto> list = new ArrayList<>();
        ResponseUtil<List<PanelDto>> responseUtil = new ResponseUtil<>();
        if(s != null){
            cate = JSONObject.parseObject(s, PanelDto.class);
            list.add(cate);
            return responseUtil.setData(list);
        }
        cate = iHomeService.recommend();
        list.add(cate);
        s = JSONObject.toJSONString(cate);
        cacheManager.setCache(GlobalConstants.RECOMMEND_PANEL_CACHE_KEY,s,GlobalConstants.RECOMMEND_CACHE_EXPIRE);
        return responseUtil.setData(list);
    }

}
